package com.wxzz.elearing.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.EnterpriseIdentityDto;
import com.wxzz.elearing.user.dto.EnterpriseOrganizationDto;
import com.wxzz.elearing.user.entity.EnterpriseIdentity;
import com.wxzz.elearing.user.entity.EnterpriseOrganization;
import com.wxzz.elearing.user.entity.WxUser;
import com.wxzz.elearing.user.mapper.EnterpriseIdentityMapper;
import com.wxzz.elearing.user.mapper.EnterpriseOrganizationMapper;
import com.wxzz.elearing.user.mapper.WxUserMapper;
import com.wxzz.elearing.user.service.IEnterpriseOrganizationService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangjunjie
 */
@Service
public class EnterpriseOrganizationServiceImpl extends ServiceImpl<EnterpriseOrganizationMapper, EnterpriseOrganization> implements IEnterpriseOrganizationService {

    @Autowired
    private EnterpriseOrganizationMapper enterpriseOrganizationMapper;

    @Autowired
    private EnterpriseIdentityMapper enterpriseIdentityMapper;

    @Override
    public ReturnVO saveEnterpriseOrganization(EnterpriseOrganization enterpriseOrganization) {
        if (enterpriseOrganization.getPid() == null) {
            throw new ServiceException("请传入pid，顶级为0");
        }
        //设置层级
        if (enterpriseOrganization.getPid() == 0) {
            enterpriseOrganization.setLevel(1);
        } else {
            EnterpriseOrganization parent = enterpriseOrganizationMapper.selectById(enterpriseOrganization.getPid());
            if (parent == null) {
                throw new ServiceException("pid对应层级不存在");
            } else {
                Integer thisLevel = parent.getLevel() + 1;
                if (thisLevel > 10) {
                    throw new ServiceException("层级不能超过10层");
                }
                enterpriseOrganization.setLevel(thisLevel);
            }
        }
        enterpriseOrganizationMapper.insert(enterpriseOrganization);
        return new ReturnVO("组织架构添加成功", enterpriseOrganization.getId());
    }

    @Override
    public ReturnVO updateEnterpriseOrganization(EnterpriseOrganization enterpriseOrganization) {
        enterpriseOrganizationMapper.updateById(enterpriseOrganization);
        return new ReturnVO("组织架构修改成功", enterpriseOrganization.getId());
    }

    @Override
    public ReturnVO deleteEnterpriseOrganizationById(Long enterpriseOrganizationId) {
        enterpriseOrganizationMapper.deleteById(enterpriseOrganizationId);
        return new ReturnVO("组织架构删除成功", enterpriseOrganizationId);
    }

    @Autowired
    private WxUserMapper wxUserMapper;

    @Override
    public ReturnVO<List<EnterpriseOrganization>> listEnterpriseOrganizationByPid(Long enterpriseId, EnterpriseOrganizationDto enterpriseOrganizationDto) {
        PageHelper.startPage(enterpriseOrganizationDto.getCurPage(), enterpriseOrganizationDto.getMaxPage());
        if (enterpriseOrganizationDto.getPid() == null) {
            throw new ServiceException("请传入pid，顶级为0");
        }
        List<EnterpriseOrganization> enterpriseOrganizations = enterpriseOrganizationMapper.selectList(new LambdaQueryWrapper<EnterpriseOrganization>()
                .eq(EnterpriseOrganization::getEnterpriseId, enterpriseId).eq(EnterpriseOrganization::getPid, enterpriseOrganizationDto.getPid())
                .like(StringUtils.isNotBlank(enterpriseOrganizationDto.getOrganizationName()), EnterpriseOrganization::getOrganizationName, enterpriseOrganizationDto.getOrganizationName()));
        for (EnterpriseOrganization enterpriseOrganization : enterpriseOrganizations) {
            if (enterpriseOrganization.getWxUserId() != null) {
                WxUser wxUser = wxUserMapper.selectById(enterpriseOrganization.getWxUserId());
                if (wxUser != null) {
                    enterpriseOrganization.setNickname(wxUser.getNickname());
                    enterpriseOrganization.setPhone(wxUser.getPhone());
                }
            }
            enterpriseOrganization.setPersonNum(wxUserMapper.selectCount(new LambdaQueryWrapper<WxUser>()
                    .eq(WxUser::getEnterpriseOrganizationId, enterpriseOrganization.getId())));
        }
        return new ReturnVO("组织架构查询成功", new JgPageVo<>(new PageInfo(enterpriseOrganizations)));
    }

    @Override
    public ReturnVO saveEnterpriseIdentity(EnterpriseIdentity enterpriseIdentity) {
        if (enterpriseIdentity.getPid() == null) {
            throw new ServiceException("请传入pid，顶级为0");
        }
        enterpriseIdentityMapper.insert(enterpriseIdentity);
        return new ReturnVO("新增组织身份成功", enterpriseIdentity.getId());
    }

    @Override
    public ReturnVO updateEnterpriseIdentity(EnterpriseIdentity enterpriseIdentity) {
        enterpriseIdentityMapper.updateById(enterpriseIdentity);
        return new ReturnVO("组织身份修改成功", enterpriseIdentity.getId());
    }

    @Override
    public ReturnVO deleteEnterpriseIdentityById(Long enterpriseIdentityId) {
        enterpriseIdentityMapper.deleteById(enterpriseIdentityId);
        return new ReturnVO("组织身份删除成功", enterpriseIdentityId);
    }

    @Override
    public ReturnVO<List<EnterpriseIdentity>> listEnterpriseIdentityByPid(Long enterpriseId, EnterpriseIdentityDto enterpriseIdentityDto) {
        PageHelper.startPage(enterpriseIdentityDto.getCurPage(), enterpriseIdentityDto.getMaxPage());
        if (enterpriseIdentityDto.getPid() == null) {
            throw new ServiceException("请传入pid，顶级为0");
        }
        List<EnterpriseIdentity> enterpriseIdentities = enterpriseIdentityMapper.selectList(new LambdaQueryWrapper<EnterpriseIdentity>()
                .eq(EnterpriseIdentity::getEnterpriseId, enterpriseId).eq(EnterpriseIdentity::getPid, enterpriseIdentityDto.getPid())
                .like(StringUtils.isNotBlank(enterpriseIdentityDto.getIdentityName()), EnterpriseIdentity::getIdentityName, enterpriseIdentityDto.getIdentityName()));
        for (EnterpriseIdentity enterpriseIdentity : enterpriseIdentities) {
            Integer count = wxUserMapper.selectCount(new LambdaQueryWrapper<WxUser>()
                    .eq(WxUser::getEnterpriseIdentityId, enterpriseIdentity.getId()));
            enterpriseIdentity.setPersonNum(count);
        }
        return new ReturnVO("组织身份查询成功", new JgPageVo<>(new PageInfo(enterpriseIdentities)));
    }

    @Override
    public ReturnVO<String> selectOrganizationById(Long organizationId) {
        EnterpriseOrganization enterpriseOrganization = enterpriseOrganizationMapper.selectById(organizationId);
        return new ReturnVO("组织架构查询成功", JSONObject.toJSONString(enterpriseOrganization));
    }
}
